package com.bw.springboot.stater.email.service;

import com.bw.springboot.stater.email.bean.MailBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;

@Slf4j
public class MailService {

    public MailService() {
        log.info("mail service 初始化成功");
    }

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String emailAddress;


    /**
     * 发送一个简单格式的邮件
     * @param mailBean
     */
    public void sendSimpleMail(MailBean mailBean) {
        if (Objects.isNull(mailBean.getSubject()) || Objects.isNull(mailBean.getRecipient()) || Objects.isNull(mailBean.getContent())) {
            log.info("邮件接收人:recipient,邮件主题:subject,邮件内容:content 不能为空");
            return;
        }
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            //邮件发送人
            simpleMailMessage.setFrom(emailAddress);
            //邮件接收人
            simpleMailMessage.setTo(mailBean.getRecipient());
            //邮件主题
            simpleMailMessage.setSubject(mailBean.getSubject());
            //邮件内容
            simpleMailMessage.setText(mailBean.getContent());
            javaMailSender.send(simpleMailMessage);
            log.info("发送邮件成功：to:{},content:{}",mailBean.getRecipient(),mailBean.getContent());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("邮件发送失败", e.getMessage());
        }
    }

    /**
     * 发送带附件格式的邮件
     * @param mailBean
     */
    public void sendAttachmentMail(MailBean mailBean) {
        if (Objects.isNull(mailBean.getSubject()) || Objects.isNull(mailBean.getRecipient()) || Objects.isNull(mailBean.getContent())
                || Objects.isNull(mailBean.getFileName()) || Objects.isNull(mailBean.getFilePath())) {
            log.info("邮件接收人:recipient,邮件主题:subject,邮件内容:content,附件名称:fileName,附件绝对路径:filePath 不能为空");
            return;
        }
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(emailAddress);
            mimeMessageHelper.setTo(mailBean.getRecipient());
            mimeMessageHelper.setSubject(mailBean.getSubject());
            mimeMessageHelper.setText(mailBean.getContent());
            //文件路径
            File file = new File(mailBean.getFilePath());
            mimeMessageHelper.addAttachment(mailBean.getFileName(), file);
            javaMailSender.send(mimeMailMessage);
            log.info("发送邮件成功：to:{},content:{}，fileName:{}",mailBean.getRecipient(),mailBean.getContent(),mailBean.getFileName());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("邮件发送失败", e.getMessage());
        }
    }
}
